#include "enter_exit.h"
#include "ui_enter_exit.h"
#include <QVBoxLayout>
#include <QDebug>
#include <QFont>
#include <QEvent>
#include <QWidgetAction>

enter_exit::enter_exit(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::enter_exit)
{
    ui->setupUi(this);
    //创建垂直布局
    QVBoxLayout* vlayout=new QVBoxLayout(this);
    //添加自定义标签
    label=new myLabel(this);
    label->setMinimumHeight(100);
    label->setMaximumHeight(100);
    label->setAlignment(Qt::AlignCenter);
    label->setStyleSheet(R"(background-color: rgb(170, 255, 127);)");
    label->setFont(QFont(QString("宋体"),20));
    //添加系统标签
    label2=new QLabel(this);
    label2->setMinimumHeight(100);
    label2->setMaximumHeight(100);
    label2->setAlignment(Qt::AlignCenter);
    label2->setStyleSheet(R"(background-color: rgb(255, 0, 0);)");
    label2->setFont(QFont(QString("宋体"),20));
    //设置事件过滤器
    label2->installEventFilter(this);

    //将标签添加到垂直布局中
    vlayout->addWidget(label);
    vlayout->addWidget(label2);
}

enter_exit::~enter_exit()
{
    delete ui;
}

static int times=0;
bool enter_exit::eventFilter(QObject *watched, QEvent *event)
{
    if(watched==label2)
    {
        if(event->type()==QEvent::Enter)//进入事件
        {
            label2->setText(QString("进入次数：%1").arg(times++));
        }
        else if(event->type()==QEvent::Leave)
        {
            label2->setText(QString("离开次数：%1").arg(times++));
        }
    }
    return QWidget::eventFilter(watched, event);
}
